3. CodeWarrior Scripting on Microsoft Windows



This chapter introduces and discusses the scripting support provided by the CodeWarrior IDE on Microsoft Windows.


CodeWarrior Windows Scripting Overview

This chapter discusses the COM scripting classes supported in CodeWarrior and how to begin using them. You should read this chapter if you would like to enhance and extend the capabilities of the CodeWarrior IDE.

By scripting the IDE using a scripting editor, it is possible to execute many CodeWarrior IDE commands without using the IDE directly. Scripting the CodeWarrior IDE is a way to automate repetitive tasks that do not need user interaction.There are many exciting things that you can do to harness the power of the IDE, such as automate builds, generate files automatically, and configure settings.


TIP:

Look at the example scripts on the CD. Reviewing these scripts will save you time when learning to write your own.

This chapter is not necessarily a tutorial. If you want to learn how to edit, save, and run scripts, you may not find the information here. Instead, refer to other tools and sources of information listed in "Tools and Reference Material" for more information.

The topics in this chapter are:


Tools and Reference Material

There are several items you will want to become acquainted with in order to effectively script the IDE:


Microsoft Scripting Technologies Web Site

You can find numerous resources and pointers to information on the world wide web at:

http://msdn.microsoft.com/scripting/

This address contains information about the Windows Scripting Host, the languages you can use for scripting, terminology, and debugging information.


OLE/COM Object Viewer

You will need a copy of the OLE/COM Object Viewer application from Microsoft in order to understand how to interpret the interfaces to the IDE. You can find this application on the world wide web at:

http://www.microsoft.com/com/resources/oleview.asp

Other editing and debugging tools may be available from third-party vendors.

To use OLE/COM Object Viewer, launch it and you will see something similar to Figure 3.1. Use the View menu to put it into Expert Mode, clickon the plus sign "+" next to Type Libraries in the left window pane, then click on the Metrowerks CodeWarrior IDE entry.


NOTE:

In Visual Studio 6.0 tools suite from Microsoft, the application is instead called OLE Viewer.

Figure 3.1 OLE/COM Object Viewer



If you double-click on the Metrowerks Codewarrior IDE entry, a window like that shown in Fig appears.

Figure 3.2 ITypeLib Viewer


This view shows all the IDL (Interface Definition Language) for the COM object. Using this information you can determine how to talk to the IDE to control its behavior through scripting.


CodeWarrior IDE COM Classes

CodeWarrior events have several classes to let you control the CodeWarrior IDE's actions and option settings.

The following is a list of all interface types in the .IDL definition for the CodeWarrior IDE application. You can inspect the details of these definitions in the OLE/COM Object Viewer application.


Listing 3.1 IDL Types for the CodeWarrior IDE


interface ICodeWarriorProject;
interface IFileSpec;
interface ICodeWarriorDesignCollection;
interface ICodeWarriorDesign;
interface ICodeWarriorTargetCollection;
interface ICodeWarriorTarget;
interface ICodeWarriorSymbolContainer;
interface ICodeWarriorClassCollection;
interface ICodeWarriorClass;
interface ICodeWarriorSymbol;
interface ICodeWarriorSourceContext;
interface ICodeWarriorBaseClassCollection;
interface ICodeWarriorBaseClassInfo;
interface ICodeWarriorDataMemberCollection;
interface ICodeWarriorDataMember;
interface ICodeWarriorMethodCollection;
interface ICodeWarriorMethod;
interface ICodeWarriorProjectFileCollection;
interface ICodeWarriorProjectFile;
interface ICodeWarriorVCSState;
interface ICodeWarriorTargetFileCollection;
interface ICodeWarriorTargetFile;
interface ICodeWarriorAccessPaths;
interface ICodeWarriorAccessPathCollection;
interface ICodeWarriorAccessPath;
interface ICodeWarriorUserTree;
interface ICodeWarriorUserTreeCollection;
interface ICodeWarriorSubTargetCollection;
interface ICodeWarriorSubTarget;
interface IFileSpecCollection;
interface IBSTRCollection;
interface IStream;
interface ISequentialStream;
interface ICodeWarriorBuildMessages;
interface ICodeWarriorMessageCollection;
interface ICodeWarriorMessage;
interface ICodeWarriorTargetOutput;
interface ICodeWarriorApp;
interface ICodeWarriorProjectCollection;
interface ICodeWarriorCreatableItemCollection;
interface ICodeWarriorCreatableItem;
interface ICodeWarriorDocumentCollection;
interface ICodeWarriorDocument;
interface ICodeWarriorProjectDocument;
interface ICodeWarriorVersionControl;
interface ICodeWarriorTextDocument;
interface ICodeWarriorTextEngine;
interface ICodeWarriorComponent;
interface ICodeWarriorComponentPropertyCollection;
interface ICodeWarriorComponentProperty;
interface ICodeWarriorComponentEventSetCollection;
interface ICodeWarriorComponentEventSet;
interface ICodeWarriorComponentEventCollection;
interface ICodeWarriorComponentEvent;
interface ICodeWarriorSymbolCollection;
interface ICodeWarriorComponentCollection;
interface ICodeWarriorAppEvents;
interface ICodeWarriorProjectEvents;
interface ICodeWarriorDesignEvents;
interface ICodeWarriorDesignAttachment;
interface ICodeWarriorCreateProjectItem;
interface ICodeWarriorCreateFileItem;
interface ICodeWarriorCreateObjectItem;
interface ICodeWarriorVCSFileStateListener;
interface ICodeWarriorProjectAssociation;
interface ICodeWarriorErrorInfo;


Sample Scripts and How to Get Started

This section will show you how to get a jump on starting to write your own scripts.

The topics in this section are:


How to Start Scripting

Note that one of the first lines of your script should be something like:


set codewarrior = CreateObject("CodeWarrior.CodeWarriorApp")

This creates a COM instance of the CodeWarrior IDE that you can interact with in subsequent scripting operations.

Now we'll analyze how you would use OLE/COM Object Viewer to learn how to script the IDE for one simple operation. In order to do much in the IDE, you will need to open a project. This can be accomplished with this script command:


set project = codewarrior.OpenProject(projectname, true, 2, 0 )

How would you know how to write this command? Simple, use the OLE/COM Object Viewer to inspect the CoClasses for the operation you want to perform.

To do this for the OpenProject method, you would first launch OLE/COM Object Viewer. Then use the View menu to put it into Expert Mode, clickon the plus sign "+" next to Type Libraries in the left window pane, then double-click on the Metrowerks CodeWarrior IDE entry. A window similar to Figure 3.2 appears. Then use the View menu of the ITypeLib Viewer window to choose Group by type kind. Then click on the plus sign "+" next to CoClasses, then the + next to coclass CodeWarriorApp, then the + next to Methods. Click on OpenProject and your window should now look something like that shown in Figure 3.3.

Figure 3.3 OpenProject Method


This shows that the OpenProject method requires 4 arguments, all are parameters that are passed in only (the [in] designation signifies this):

You can view the values for the enumerations in the same window under the Enums hierarchy.


Script Examples

Here are a couple of example scripts that illustrate the way you might want to write your scripts. These scripts are invoked from the command-line (Start/Programs/MS-DOS Prompt) using the wscript executable.

The scripts in this section are:


Removing Object Code

This script accepts as the command-line parameter the path to the project to be opened. The absolute path needs to be included, for example:


wscript select~1.vbs "C:\testproejcts\test1.mcp"

If no command line arguments are given, the script prompts the user for the absolute path of the project file to be opened. If specified, the script tries to open the project, otherwise it opens the default one c:\testprojects\test1.mcp. This script opens the project and selects the files that belong to the default target.


Listing 3.2 RemoveObjectCode.vbs


option explicit

'*******Variable declaration
dim codewarrior
dim project
dim projectname
dim targetIntf
dim count
dim projectCollection
dim targetcollection
dim result
dim showinputbox
dim objArgs

'****** Script ********
Set objArgs = Wscript.Arguments
projectname = "c:\testprojects\test1.mcp"

if objArgs.Count > 1 then
	MsgBox "This Script expects only one argument, rest of the arguments will be ignored!!"
	showinputbox = false
	projectname = CStr(objArgs(0))
end if

if objArgs.Count = 0 then 
	showinputbox = true
else
	showinputbox = false
	projectname = CStr(objArgs(0))
end if

if showinputbox = true then
	result = InputBox("Enter the absolute path for the project to be opened","Input", projectname, 100, 100)
	
	If result = "" Then
		projectname = "c:\testprojects\test1.mcp"
	else
		projectname = cstr(result)
	end if
end if

'Create automation app object
set codewarrior = CreateObject("CodeWarrior.CodeWarriorApp")
MsgBox "App Created"

project = Null
'open project 
set project = codewarrior.OpenProject(projectname, true, 2, 0 )
if TypeName( project ) <> "Null"  then
	set targetcollection = project.Targets
	count = targetcollection.Count

	IF  ( count > 0 ) then
		set targetIntf = targetcollection.Item( 0 )
		targetIntf.RemoveObjectCode( true )
	END IF
else
	MsgBox CStr( projectname & " does not exist" )
end if 


Build and Wait

This script accepts as commandline parameter the name of the project to be opened. The absolute path needs to be included, for example:


wscript select~1.vbs "C:\testprojects\test1.mcp"

If no command-line arguments are given, the script prompts the user for the absolute path of the project file to be opened. If specified, the script tries to open the project, else opens te default one "c:\testprojects\test1.mcp".


Listing 3.3 BuildAndWait.vbs


option explicit

'*******Variable declaration
dim codewarrior
dim project
dim projectname
dim targetIntf
dim count
dim projectCollection
dim targetcollection
dim result
dim showinputbox
dim objArgs
dim buildErrors

'****** Script ********
Set objArgs = Wscript.Arguments
projectname = "c:\temp\none\none.mcp"

if objArgs.Count > 1 then
	MsgBox "This Script expects only one argument, rest of the arguments will be ignored!!"
	showinputbox = false
	projectname = CStr(objArgs(0))
end if

if objArgs.Count = 0 then 
	showinputbox = true
else
	showinputbox = false
	projectname = CStr(objArgs(0))
end if

if showinputbox = true then
	result = InputBox("Enter the absolute path for the project to be opened","Input", projectname, 100, 100)
	
	If result = "" Then
		projectname = "c:\testprojects\test1.mcp"
	else
		projectname = cstr(result)
	end if
end if

'Create automation app object
set codewarrior = CreateObject("CodeWarrior.CodeWarriorApp")
MsgBox "App Created"

project = Null
'open project 
set project = codewarrior.OpenProject(projectname, true, 2, 0 )
if TypeName( project ) <> "Null"  then
  project.BuildAndWaitToComplete
else
	MsgBox CStr( projectname & " does not exist" )
end if 

project.close






Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: support@metrowerks.com
Copyright © 1999, Metrowerks Corp. All rights reserved.

Last updated: May 25, 1999 * Chris Magnuson * John Roseborough